public class adduserext {

    public adduserext() {

    }


// Class variables
        public List<UserWrapcls> UserList = new List<UserWrapCls>(); 
        List <String> SelectedUsers = new List<String>();
        public List <user> totalusers {get;set;}
        public UserWrapCls UserWrap {get;set;}
        public id a;
        public static boolean pageblock1 = true;
        public static boolean pageblock2 = false;
// Constructor
        public adduserext(ApexPages.StandardController controller) {
          
          a = ApexPages.CurrentPage().getParameters().get('Id');
          system.debug ('^^^^^^'+a);
         }
// For rendering page block in visualforce page
        public Boolean getpageblock1()
         {
          return pageblock1;
         }
        public Boolean getpageblock2()
          {
           return pageblock2;
          }
// List of All the active users
       public List<UserWrapcls> getUserList() {
       totalusers = new List<user>([select id, name,Email from user where IsActive = True limit 10]);
       for (User i : totalusers)
         {
           UserWrap = new UserWrapCls(i);
           UserList.add(UserWrap);
         }
       return UserList;
        }

// Method for sending mail after clicking on the save button
  
  public pagereference Useremail()
  {
    for(UserWrapcls i : UserList)
     {
       
 // To find out the selected users
         if(i.IsSelected)
           {
           SelectedUsers.add (i.activeusers.Email);
           }
      }
        system.debug ('#########'+ SelectedUsers);
        system.debug ('SizeSize'+ SelectedUsers.size());
 
 //Checking the number of users selected
         If ((SelectedUsers.size())< 2 && (SelectedUsers.size())>0 )
          {
             pageblock2 = false;
             pageblock1 = true;

//Sending Mail to selected users
            Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();
            mail.setToAddresses(SelectedUsers);
            mail.setHtmlBody ('This is a new object created');
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
            pagereference pagerfrn = new pagereference ('/'+a);
            return pagerfrn;
           }
         else
           {
            pageblock2 = true;
            pageblock1 = true;
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'You canot select more than 10 users');
            ApexPages.addMessage(myMsg);
            UserList.clear();
            SelectedUsers.clear();
            return null;
           }
     } 

//Wraper class
public class UserWrapcls
{
public boolean IsSelected {get;set;}
public user activeusers {get;set;}
public UserWrapcls (User u)
{
activeusers = new user();
this.activeusers = u;
IsSelected = False;
}
}
//end of wrap class
}